package com.example.children.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.children.FileService.LoginInterface;
import com.example.children.Mapper.UserMapper;
import com.example.children.Model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class LoginService extends ServiceImpl<UserMapper, User> implements LoginInterface {
    @Autowired
    private  UserMapper userMapper;

    // 验证用户名和密码
    public  User validateCredentials(User user) {
        // 1. 查找用户
        System.out.println("用户名："+user.getUsername());
        System.out.println("密码："+user.getPassword());
        boolean test = userMapper.findByUsernameBool(user.getUsername());
        if (!test) {
            System.out.println("登录：用户不存在");
            return null;  // 用户不存在
        }
//        String md5Password = MD5Util.encrypt(user.getPassword());
//        user.setPassword(md5Password);
        User isPasswordMatch = userMapper.matches(user);

        if (isPasswordMatch==null) {
            System.out.println("登录：密码不正确");
            return null;  // 密码不正确
        }
        // 密码正确，返回用户信息
        return isPasswordMatch;
    }


    public void saveInvitationCode(User user) {
        userMapper.updateInvitationCode(user);
    }
}
